package com.aidex.biz.controller;

import java.util.List;

import com.aidex.biz.domain.Customer;
import com.aidex.biz.domain.XiansuoChengben;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.domain.R;
import com.github.pagehelper.PageInfo;
import com.aidex.common.core.page.PageDomain;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.poi.ExcelUtil;

import javax.validation.constraints.*;

import org.springframework.web.bind.annotation.*;
import com.aidex.common.core.controller.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.aidex.biz.domain.CustomerTag;
import com.aidex.biz.service.CustomerTagService;
import com.aidex.common.utils.SecurityUtils;

/**
 * 客户标签信息Controller
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2025-04-01
 */
@RestController
@RequestMapping("/biz/bizCustomerTag")
public class CustomerTagController extends BaseController {
    @Autowired
    private CustomerTagService customerTagService;

    /**
     * 查询客户标签信息列表
     */
    @GetMapping("/list")
    public R<PageInfo> list(CustomerTag customerTag, HttpServletRequest request, HttpServletResponse response) {
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            customerTag.setCompanyId(null);
        } else {
            customerTag.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        customerTag.setPage(new PageDomain(request, response));
        return R.data(customerTagService.findPage(customerTag));
    }

    /**
     * 获取客户标签信息详细信息
     */
    @GetMapping(value = "/{id}")
    public R<CustomerTag> detail(@PathVariable("id") String id) {
        return R.data(customerTagService.get(id));
    }

    /**
     * 新增客户标签信息
     */
    @Log(title = "客户标签信息", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated CustomerTag customerTag) {
        return R.status(customerTagService.save(customerTag));
    }

    /**
     * 修改客户标签信息
     */
    @Log(title = "客户标签信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated CustomerTag customerTag) {
        return R.status(customerTagService.save(customerTag));
    }


    /**
     * 删除客户标签信息
     */
    @Log(title = "客户标签信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(customerTagService.deleteCustomerTagByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(customerTagService.findMaxSort(new CustomerTag()));
    }

    /**
     * 导出客户标签信息列表
     */
    @Log(title = "客户标签信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(CustomerTag customerTag) {

        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            customerTag.setCompanyId(null);
        } else {
            customerTag.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        List<CustomerTag> list = customerTagService.findList(customerTag);
        ExcelUtil<CustomerTag> util = new ExcelUtil<CustomerTag>(CustomerTag.class);
        return util.exportExcel(list, "客户标签信息数据");
    }

    /**
     * 批量新增标签
     */

    @Log(title = "客户标签信息", businessType = BusinessType.INSERT)
    @PostMapping("/addBatch")
    public R addBatch(@RequestBody @Validated Customer customer) {
        return R.status(customerTagService.addBatch(customer));
    }
}
